<?php
require_once 'const.inc';
require_once 'Zend/Loader.php';
require_once 'lib/SessionManager.php';
require_once 'lib/Global.php';

$GLOBALS['_DBG'] = 0;

class Bootstrap
{
    public static $frontController = null;

    public static $root = '';

    public static $registry = null;

    public static function run()
    {
        self::prepare();
        $response = self::$frontController->dispatch();
        self::sendResponse($response);
        if ($GLOBALS['_DBG']==1)
        {
			self::renderDevel();
		}
    }

    public static function prepare()
    {
        self::setupEnvironment();
        Zend_Loader::registerAutoload();
        self::setupRegistry();
        self::setupConfiguration();
        self::setupFrontController();
        self::setupView();
        self::setupActionHelper();
        self::setupDatabase();
        self::setupSession();
    }

    public static function setupEnvironment()
    {
		$d=date("Ymd", time());
		$dbg=$d . "dbg";
		if (isset($_COOKIE['_DBG']))
		{
			if ($_COOKIE['_DBG']==$dbg)
			{
				$GLOBALS['_DBG']=1;
			}
		}
		if (isset($_GET['_DBG']))
		{
			if ($_GET['_DBG']==$dbg && $GLOBALS['_DBG']==0)
			{
				$GLOBALS['_DBG']=1;
				setcookie("_DBG", $dbg, time() + 1800, "/", TAMTAY_COOKIE_DOMAIN);  
			}
			else if ($GLOBALS['_DBG']==1 && $_GET['_DBG']!=$dbg)
			{
				$GLOBALS['_DBG']=0;
				setcookie("_DBG", $dbg, time() - 1800, "/", TAMTAY_COOKIE_DOMAIN);  
			}
		}     		
		ini_set('display_startup_errors', $GLOBALS['_DBG']==1);
        self::$root = dirname(dirname(__FILE__));
    }

    public static function setupFrontController()
    {
        self::$frontController = Zend_Controller_Front::getInstance();  
        self::$frontController->returnResponse(true);   
		$paths = array('default' => APPLICATION_PATH . DS .'controllers');
        self::$frontController->setControllerDirectory($paths);
		//self::$frontController->setDefaultControllerName("index");
    }

    public static function setupView()
    {
		Zend_Layout::startMvc(array("layoutPath" => APPLICATION_PATH . '/layouts/scripts', "layout" => "default"));
		$view = Zend_Layout::getMvcInstance()->getView();
		$view->doctype('XHTML1_STRICT');
		$view->addHelperPath(APPLICATION_PATH  . '/views/helpers', 'TT_View_Helper');
    }

    public static function sendResponse(Zend_Controller_Response_Http $response)
    {
        $response->setHeader('Content-Type', 'text/html; charset=UTF-8', true);
        $response->sendResponse();
    }

    public static function setupRegistry()
    {
		self::$registry = new Zend_Registry(array(), ArrayObject::ARRAY_AS_PROPS);
		Zend_Registry::setInstance(self::$registry);
		
		//user
		$user=(object)null;
		$user->uid=0;
		$user->name="";
		$user->avatar="/themes/mytt_themes/images/nophoto.gif";
		self::$registry->user=$user;
		
		//variables
		self::$registry->variables=null;
    }

    public static function setupConfiguration()
    {
		$config = new Zend_Config(require APPLICATION_PATH . '/config/tamtay.php');
        self::$registry->configuration = $config;
    }

    public static function setupDatabase()
    {
        $config = self::$registry->configuration;
		$dbAdapters = array();
        
        $session_db = Zend_Db::factory($config->session_db);
		$session_db->getProfiler()->setEnabled($GLOBALS['_DBG']==1);
		$session_db->setFetchMode(Zend_Db::FETCH_OBJ);
		Zend_Db_Table::setDefaultAdapter($session_db);
        self::$registry->session_db = $session_db;
    }
    
    public static function setupActionHelper()
    {
		Zend_Controller_Action_HelperBroker::addPath(APPLICATION_PATH . '/helpers', 'Helper');
    }
    
	public static function setupSession()
	{
		$session_db = self::$registry->session_db;
		
		Zend_Session::setOptions(array(
						'gc_probability' => 1,
						'gc_divisor' => 5000
						));
		
		$sessionManager = new SessionManager();
		$sessionManager->db=$session_db;
		
		Zend_Session::setSaveHandler($sessionManager);	
		
		Zend_Session::setOptions(array("cookie_domain" => TAMTAY_COOKIE_DOMAIN, "name" => TAMTAY_SESSION_NAME));
		if ($_SERVER['REQUEST_METHOD']=='POST')
		{
			if (isset($_POST[session_name()]))
			{
				$sid=$_POST[session_name()];
				$_COOKIE[session_name()]=$sid;
				Zend_Session::setId($sid);
			}
		}
		Zend_Session::start();
	}
	
	public static function renderDevel()
	{
		
		$profilers=array();
		
		$db=Zend_Registry::get("session_db");
		$profiler=$db->getProfiler();	
		$profilers["session_db"]=$profiler;
		
		$view = new Zend_View(array('basePath' => APPLICATION_PATH . '/views'));
		$view->profilers = $profilers;
		echo $view->render('devel.phtml');
		
	}
}